﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.weixin.Material
{
    public partial class EditTextMaterialDialog : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected WxMaterial _materialDb;

        protected WxMaterialText _textDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _userAccount = UserKit.GetUserAccountFailLogin();
            _frameHelper = new FrameHelper(this);
            if (!IsPostBack)
            {
                ViewState["uid"] = Request["uid"];
            }
            string _uid = (string)ViewState["uid"];

            if (!string.IsNullOrEmpty(_uid) && _uid != "0")
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    try
                    {
                        if (!IsPostBack)
                        {
                            ViewState["uid"] = Request["uid"];
                        }

                        _materialDb = session.CreateCriteria<WxMaterial>().Add(Expression.Eq("Id", int.Parse(_uid))).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterial>();

                        if (_materialDb == null)
                        {
                            throw new Exception("找不到对应信息.");
                        }

                        _textDb = session.CreateCriteria<WxMaterialText>().Add(Expression.Eq("Id", _materialDb.Id)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterialText>();

                        if (_textDb == null)
                        {
                            throw new Exception("找不到对应信息.");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                        return;
                    }
                }
            }
            if (_materialDb == null)
            {
                _materialDb = new WxMaterial();
            }
            if (_textDb == null)
            {
                _textDb = new WxMaterialText();
            }
        }
    }
}